/**
 * appRain v 0.1.x
 *
 * LICENSE
 *
 * This source file is subject to the MIT license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.opensource.org/licenses/mit-license.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@apprain.com so we can send you a copy immediately.
 *
 * @copyright  Copyright (c) 2010 appRain, Team. (http://www.apprain.com)
 * @license    http://www.opensource.org/licenses/mit-license.php MIT license
 *
 * HELP
 *
 * Official Website
 * http://www.apprain.com/
 *
 * Download Link
 * http://www.apprain.com/download
 *
 * Documents Link
 * http ://www.apprain.com/docs
 */

window.addEvent('domready', function() {

	var row_id;
	var deletemessage = "Are you sure to Delete?\n\nThere is currently a request to delete data from the sytem. You may can not undone.\n\nPress OK to continue, or Cancel to stay on same position.";
	
	var req = new Request({url:base,
		onSuccess: function(txt) {
			$(row_id).setStyle("display","none")
		},
		onFailure: function() {
			alert("Sorry... Commond could not execute");
		}
	});
	
	if($chk($('checkall')))
	{
		$('checkall').addEvent('click',function (e) {
			var toggle = $('checkall').checked;
			$$('input[class=checkrow]').each(function(check) {
				check.checked = toggle;
			});
		});
		
		$$('.checkrow').addEvent('click', function(e) {
			$('checkall').checked = false;
		});
		
		$$('._deletebtn').addEvent('click', function(e) {
			
			var opts = JSON.decode($('checkall').get('class'));
			var ids = "";
			$$('.checkrow').each(function(obj,key){
				if(obj.checked)
				{
					ids += ((ids=="")?"":",") + obj.value;
				}
			});
			if(ids!="")
			{
				if(confirm(deletemessage)){
				var req = new Request({url:base,
					method: 'post',
					data:{'ids':'{' + ids + '}'},
					onSuccess: function(txt) {window.location="";},
					onFailure: function() {alert("Sorry... Commond could not execute");}
				});
				req.send({url:base + "/common/deletegroup/" + opts.mode });}
			}	
		});
	}

	$$('.link_delete').addEvent('click', function(e) {

		var model = this.title.split('_')[0];
		var id = this.title.split('_')[1];
		row_id = this.getParent("tr").get("id");

		var highlight = new Fx.Morph(row_id, {
		duration: 1500,
		link: 'cancel',
		transition: 'quad:out'
		});

		var orginal_color = $(row_id).getStyle('background-color');

		highlight.start({
				backgroundColor: ['#fff36f', '#FFFF1A' ],
				opacity: [.5, 1]
		});


		if(confirm(deletemessage))
		{
			req.send({url:base + "/common/delete_row/" +  model + "/" + id });
		}
		else
		{
			highlight.start({
				backgroundColor: ['#FFFF1A',orginal_color ],
				opacity: [.5, 1]
			});

		}
	});

	if($chk($$('.delete_informationset_file')))
	{
		$$('.delete_informationset_file').addEvent('click', function (e) {
			var obj = this.getParent('span');
			this.src = base + '/images/loading.gif';
			this.setStyle('margin-left','5px');
			var req = new Request({url:base,
				onSuccess: function(txt) {
					obj.setStyle('display','none');
				},
				onFailure: function() {
					alert("Sorry... Commond could not execute");
				}
			});
			req.send({url:base + "/common/delete_row/delete_informationset_file/" + this.id });
		});	
	}
});